﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using CCWin;
using BookModel;

namespace WinFromBook
{
    public partial class FrmBookInsert : CCSkinMain
    {
        public FrmBookInsert()
        {
            InitializeComponent();
        }
        #region 封装方法，字段，执行操作


        public int BID { get; set; }
        public string Btype { get; set; }
        public string BRNaem { get; set; }
        public string Bname { get; set; }
        public FrmBookInfo fbi { get; set; }

        //下拉框加载
        public void ComLoad()
        {
            this.skinComboTYpe.DataSource = new BLL.TypeInfoBLL().GetTYpeInfo();
            this.skinComboTYpe.DisplayMember = "TName";
            this.skinComboTYpe.ValueMember = "TID";
            this.skinComboName.DataSource = new BLL.ReadingInfoBLL().GetReading();
            this.skinComboName.DisplayMember = "RName";
            this.skinComboName.ValueMember = "RID";
        }
        //图书信息加载
        public void GetBookInfo()
        {
            foreach (var item in new BLL.BookInfoBLL().BookInfoAll())
            {
                if (item.Bname == Bname)
                {
                    BID = item.BID;
                    skinName.Text = item.Bname;
                    skinAuthor.Text = item.BAthour;
                    skinPUb.Text = item.BPulish;
                    skinPrice.Text = item.BPrice + "";
                    skinCover.Text = item.Baddress;
                    rtfRichMain.Text = item.BMain;
                    skinDataTime.Value = item.BDate;
                }
            }

          this.skinComboTYpe.Text= Btype;
          this.skinComboName.Text = BRNaem;
        }
        //判断文本框是否为空
        public bool CheckTextBox()
        {
            if (String.IsNullOrEmpty(skinName.Text.Trim()))
            {
                MessageBox.Show("请输入图书名称", "系统提示");
                return false;
            }
            if (String.IsNullOrEmpty(skinAuthor.Text.Trim()))
            {
                MessageBox.Show("请输入图书作者", "系统提示");
                return false;
            }
            if (String.IsNullOrEmpty(skinPUb.Text.Trim()))
            {
                MessageBox.Show("请输入图书出版社", "系统提示");
                return false;
            }
            if (String.IsNullOrEmpty(skinPrice.Text.Trim()))
            {
                MessageBox.Show("请输入图书价格", "系统提示");
                return false;
            }
            if (String.IsNullOrEmpty(skinCover.Text.Trim()))
            {
                MessageBox.Show("请输入图书存放位置", "系统提示");
                return false;
            }
            if (String.IsNullOrEmpty(rtfRichMain.Text.Trim()))
            {
                MessageBox.Show("请输入图书简介", "系统提示");
                return false;
            }
            return true;
        }

        //图书修改与新增事件
        public void InsertOrUpdate()
        {
            if (CheckTextBox())
            {
                BookInfo bf = new BookInfo
                {
                    BID = BID,
                    Bname = skinName.Text.Trim(),
                    BAthour = skinAuthor.Text.Trim(),
                    BPulish = skinPUb.Text.Trim(),
                    BPrice = decimal.Parse(skinPrice.Text.Trim()),
                    BDate = skinDataTime.Value,
                    Baddress = skinCover.Text.Trim(),
                    BMain = rtfRichMain.Text.Trim(),
                    TID = int.Parse(skinComboTYpe.SelectedValue.ToString()),
                    RID = skinComboName.SelectedValue.ToString()
                };
                string sql = "";
                if (this.Text == "图书更新")
                {
                    sql = new BLL.BookInfoBLL().UpdateBookInfo(bf);
                    List<BookInfo> Lb = new BLL.BookInfoBLL().GetBookInfo();
                    fbi.BookInfoShow(Lb);
                    this.Close();
                }
                else
                {
                    sql = new BLL.BookInfoBLL().insertBookInfo(bf);
                    if (fbi!=null)
                    {
                        List<BookInfo> Lb = new BLL.BookInfoBLL().GetBookInfo();
                        fbi.BookInfoShow(Lb);
                    }
                }
                MessageBox.Show(sql, "系统提示");
                ClearTextBox();
            }
        }
        //清空文本框方法
        public void ClearTextBox()
        {
            skinName.Text = "";
            skinAuthor.Text = "";
            skinPUb.Text = "";
            skinPrice.Text = "";
            skinDataTime.Value = DateTime.Now;
            skinCover.Text = "";
            rtfRichMain.Text = "";
            skinComboTYpe.SelectedIndex = 0;
            skinComboName.SelectedIndex = 0;
        }
        #endregion
        private void FrmBookInsert_Load(object sender, EventArgs e)
        {
            ComLoad();
           
            if (this.Text == "图书更新")
            {
                GetBookInfo();
            }
        }

        private void skinButtonAdd_Click(object sender, EventArgs e)
        {
            InsertOrUpdate();
        }

        private void skinButtonReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void skinButtonReader_Click(object sender, EventArgs e)
        {
            ClearTextBox();
        }
        public bool TextCheck(KeyPressEventArgs e)
        {
            //数字，删除键
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') ||(e.KeyChar== '.') || ((Keys)(e.KeyChar) == Keys.Back))
            {
                return false;
            }
            return true;

        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = TextCheck(e);
        }
    }
}
